package com.gitee.springboot_userdemo.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("toload")
    public String toload(){
        return "upload";
    }

   @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request) throws IOException{
       System.out.println("查询");
        //获取文件的原始名
       String filename = uploadFile.getOriginalFilename();
       System.out.print(filename);
       //根据相对路径获取绝对路径
       String realPath = request.getSession().getServletContext().getRealPath("/upload");
       System.out.println(realPath);
       uploadFile.transferTo(new File(realPath,filename));
       return "success";
   }
}
